package com.niit.controller;

import com.niit.entity.Region;
import com.niit.service.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/region")
public class RegionController {
	@Autowired
	private RegionService regionService;

	@PostMapping("/findRegion")
	public List<Region> findRegion(@RequestBody Region region) {
		try {
			Long code = region.getCode();
			if(code == ((long)-1)) return regionService.findRegionByLevel((byte) 1);
			else return regionService.findRegionByParentCode(code);
		}catch (Exception e){
			return null;
		}
	}

	@PostMapping("/findRegionByLevel")
	public List<Region> findRegionByLevel(@RequestParam Byte level) {
		return regionService.findRegionByLevel(level);
	}

	@GetMapping("/getRegion/{id}")
	public Region getRegion(@PathVariable Long id){
		return regionService.findRegionById(id);
	}
}
